// 审核计划表
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="年份" prop="planYear">
        <el-date-picker type="year" v-model="queryParams.planYear" format="yyyy" value-format="yyyy"
          :style="{ width: '100%' }" placeholder="请选择年度" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['inAudit:inAudit:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['inAudit:inAudit:edit']">修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['inAudit:inAudit:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBox"
          v-hasPermi="['inAudit:inAudit:print']">打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" border :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column min-width="200" show-overflow-tooltip label="年度">
        <template slot-scope="scope">
          <span>{{
            scope.row.innerAuditPlan.planYear
          }}年度内部工作计划</span>
        </template>
      </el-table-column>
      <el-table-column min-width="200" show-overflow-tooltip label="内审时间">
        <template slot-scope="scope">
          <span>{{ scope.row.recordStartTime }}~{{
            scope.row.recordEndTime
          }}</span>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="审核要素" prop="element" min-width="120" />
      <el-table-column show-overflow-tooltip label="被审核部门" prop="innerAuditPlan.deptName" min-width="120" />
      <el-table-column show-overflow-tooltip label="内审组成员" min-width="150" prop="leaderName">
        <template slot-scope="scope">
          {{ scope.row.userList.find((i) => i.innerAuditPlanUserId == scope.row.innerAuditPlanUserId).userName }}
          <!-- {{scope.row.userList[scope.$index].userName }} -->
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="内审组组长" min-width="120" prop="innerAuditPlan.leaderName" />
      <el-table-column show-overflow-tooltip label="首次会议签到表" cneter="left" min-width="180">
        <template slot-scope="scope">
          <el-link v-if="scope.row.firstSignFile" @click="look(baseApi + scope.row.firstSignFile)" type="primary"
            :underline="false" target="_blank">
            <span class="el-icon-document"> 查看首次会议签到表 <i class="el-icon-edit"></i> </span>
          </el-link>
          <!-- <el-link v-if="scope.row.firstSignFile" @click="handleFile(scope.row, 1)" type="primary">
            查看首次会议签到表 <i class="el-icon-edit" @click.stop="handleFile(scope.row, 1, true)"></i>
          </el-link> -->
          <div v-else>暂未上传</div>
        </template></el-table-column>
      <el-table-column show-overflow-tooltip label="末次会议签到表" min-width="180" prop="endSignFile" cneter="left">
        <template slot-scope="scope">
          <el-link v-if="scope.row.endSignFile" type="primary" @click="look(baseApi + scope.row.endSignFile)">
            查看末次会议签到表 <i class="el-icon-edit"></i>
          </el-link>
          <div v-else>暂未上传</div>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column cneter="left" show-overflow-tooltip label="总结报告" min-width="180" prop="report">
        <template slot-scope="scope">
          <el-link v-if="scope.row.report" @click="handleFile(scope.row, 3)" type="primary">
            查看总结报告<i class="el-icon-edit" @click.stop="handleFile(scope.row, 3, true)"></i>
          </el-link>
          <div v-else>暂未上传</div>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.effectStatus" :active-value="1" :inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['inAudit:inAudit:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['inAudit:inAudit:edit']">修改</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleDelete" icon="el-icon-delete"
                v-hasPermi="['inAudit:inAudit:remove']">删除</el-dropdown-item>
              <el-dropdown-item command="handleCorrent" icon="el-icon-delete"
                v-hasPermi="['inAudit:inAudit:add:jc']">创建纠错</el-dropdown-item>

              <el-dropdown-item v-hasPermi="['inAudit:inAudit:frist:file']" command="handleFileFirst"
                icon="el-icon-plus">{{
                  !scope.row.firstSignFile ? "上传" : "查看"
                }}首次签到表</el-dropdown-item>
              <el-dropdown-item v-hasPermi="['inAudit:inAudit:last:file']" command="handleFileLast" icon="el-icon-plus">{{
                !scope.row.endSignFile ? "上传" : "查看"
              }}末次签到表</el-dropdown-item>
              <el-dropdown-item v-hasPermi="['inAudit:inAudit:report:file']" command="handleSummary"
                v-if="!scope.row.report" icon="el-icon-plus">{{ !scope.row.report ? "上传" : "查看" }}总结</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :visible.sync="openFile" :title="type == 1 ? '首次签到表' : type == 2 ? '末次签到表' : '总结报告'" width="800"
      append-to-body>
      <el-form :disabled="isEdit" ref="elForm" :model="formData" :rules="rules" size="medium" label-width="120px">
        <el-form-item label="" prop="filePath">
          {{ formData.filePath }}
          <fileUpload :isDelete="!isEdit" v-model="formData.filePath"></fileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isEdit">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="openFile = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 打印模板位置 -->
    <PrintBox :checkNum="9" :checkLabel="'innerCheckItems'" :printRow="printList" :isPageSize="false" :isVertical="false"
      id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 10px">
          管理体系内审核查表 STET/B/12-2
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse; table-layout: fixed" border="1" cellspacing="0" class="table-page">
          <tr>
            <td style="width: 40mm">条款</td>
            <td style="width: 100mm">核查内容</td>
            <td style="width: 80mm">
              审核部门
            </td>
            <td style="width: 50mm">内审结果</td>
            <td style="width: 100mm">内审记录</td>
          </tr>
          <!-- <tr >
            <td>4.1</td>
          </tr> -->
          <tr v-for="(e, i) in 2">
            <td colspan="8">4.2</td>
          </tr>
          <tr v-for="(e, i) in 9">
            <td>{{ scope.item.innerCheckItems[i] && scope.item.innerCheckItems[i].orderContent }}</td>
            <td>{{ scope.item.innerCheckItems[i] && scope.item.innerCheckItems[i].content }}</td>
            <td>{{ scope.item.innerCheckItems[i] && scope.item.innerCheckItems[i].deptName }}</td>
            <td>{{ scope.item.innerCheckItems[i] && scope.item.innerCheckItems[i].result }}</td>
            <td>{{ scope.item.innerCheckItems[i] && scope.item.innerCheckItems[i].log }}</td>
          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="desc" style="font-weight:900">
          符合”，N/A表示’不适用‘。当用‘、N、N/A表示时必须同时在”评审说明“’中详细描述。“
        </div>
        <div class="desc" style="font-weight:900">
          注：有下划线部分为结合”检验检测机构资质认定评审准则“条款特殊内容。
        </div>
        <div class="desc" style="display: flex;
    text-align: left;;
    justify-content: space-around;
    margin-top: 17px;
}">
          <!-- <div style="width:50%">内审员： {{ scope.item.userList.map((e) => e.userName).toString() }}</div> -->
          <div style="width:50%">内审员： <img style="width: 50px;" :src="$VUE_APP_IMG_API + scope.item.esign" /></div>

          <div style="width:50%">内审组长： <img style="width: 50px;" :src="$VUE_APP_IMG_API + scope.item.leadESign" /></div>
        </div>
        <div class="desc" style="width:50%">内审日期: {{ parseTime(scope.item.recordStartTime, "{y}年{m}月{d}日") }}至{{
          parseTime(scope.item.recordEndTime, "{y}年{m}月{d}日")
        }}</div>

        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import {
  listcheck,
  delcheck,
  effectcheck,
  signFile,
} from "@/api/quality/check.js";
import printStyle from "@/utils/print.js";

export default {
  name: "check",
  data() {
    return {
      formData: { filePath: undefined },
      rules: {
        filePath: [
          {
            required: true,
            message: "附件不能为空",
            trigger: "change",
          },
        ],
      },
      baseApi: this.$VUE_APP_IMG_API,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planYear: undefined,
      },
      // 日期范围
      dateRange: [],
      openFile: false,
      type: 1,
      current: {},
      infos: [], //勾选的数据
      printList: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    console.log(JSON.parse(str));
    if (str) {
      this.queryParams = JSON.parse(str);
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.infos.forEach((item, index) => {
          item.innerCheckItems.forEach((e, i) => {
            item.element.split(',').forEach((lab, val) => {
              // console.log(lab, e.orderContent);
              if (lab.indexOf(e.orderContent.substring(0,3)) != -1) {
                console.log('+++++++++++>',lab,e.orderContent.substring(0,3));
              }
            })
          })
        })
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          height:12mm;
          padding: 4px;
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.innerCheckId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.infos = selection;

    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));

      const configId = row.innerCheckId || this.ids;
      this.$router.push("./addInAudit?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));

      this.$router.push("./addInAudit?isInfo=true&id=" + row.innerCheckId);
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleFileFirst":
          this.handleFile(row, 1);
          break;
        case "handleFileLast":
          this.handleFile(row, 2);
          break;
        case "handleSummary":
          this.handleFile(row, 3);
          break;
        case "handleCorrent":
          this.handleCorrent(row);
          break;

      }
    },
    handleCorrent(e) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push('./addCorrect?innerCheckId=' + e.innerCheckId)
    },
    handleFile(row, type, isEdit = false) {

      try {
        this.formData = { filePath: undefined };
        this.type = type;
        this.current = row;
        this.isEdit = false;
        if (this.type == 1 && this.current.firstSignFile) {
          this.formData.filePath = this.current.firstSignFile;
          this.isEdit = true;
        }
        if (this.type == 2 && this.current.endSignFile) {
          this.formData.filePath = this.current.endSignFile;
          this.isEdit = true;
        }
        if (this.type == 3 && this.current.report) {
          this.formData.filePath = this.current.report;
          this.isEdit = true;
        }
        if (isEdit) this.isEdit = false
      } catch (error) {
        console.log(error)
      }
      this.openFile = true;
    },
    handleStatusChange(row) {
      let text = row.effectStatus == "1" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.innerCheckId + '"数据吗？')
        .then(function () {
          return effectcheck(row.innerCheckId, row.effectStatus);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.effectStatus = row.effectStatus == "0" ? 1 : 0;
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.innerCheckId || this.ids;

      this.$modal
        .confirm('是否确认删除' + row.innerAuditPlan.planYear + '年度内部工作计划数据项？')
        .then(function () {
          return delcheck(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addInAudit");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listcheck(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startAuditDate",
          "endAuditDate"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.dataList, ' this.dataList');
      });
    },
    look(e) {
      window.open(e)
    },
    handleExport() {
      this.download(
        "system/check/export",
        {
          ...this.queryParams,
        },
        `内部审核记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.formData.type = this.type;
          this.formData.innerCheckId = this.current.innerCheckId;
          signFile(this.formData).then((response) => {
            this.$modal.msgSuccess("操作成功");
            this.openFile = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
